package com.aaa.qy127.service.impl;

import com.aaa.qy127.dao.DingDanDao;
import com.aaa.qy127.dao.GWCDao;
import com.aaa.qy127.dao.impl.DingDanDaoImpl;
import com.aaa.qy127.dao.impl.GWCDaoImpl;
import com.aaa.qy127.entity.DingDan;
import com.aaa.qy127.entity.Dl;
import com.aaa.qy127.entity.GWC;
import com.aaa.qy127.service.DingDanService;
import com.aaa.qy127.util.CodeAndMsg;
import com.aaa.qy127.util.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service("dingDanServiceImpl")
public class DingDanServiceImpl implements DingDanService {

    @Resource(name = "dingDanDaoImpl")
    private DingDanDao dingDanDao;

    @Resource(name = "gwcDaoImpl")
    private GWCDao gwcDao;

    /**
     * 先 向 订单详情 表中添加 一条 数据 返回 主键
     * 然后 向 订单详情表中 添加 数据
     * @param bookid
     * @param count
     * @param outprice
     * @param request
     * @return
     */
    @Override
    public CodeAndMsg insertDingDan(String bookid, String count, String outprice, HttpServletRequest request) {
        Dl dl = (Dl)request.getSession().getAttribute("dl");
        CodeAndMsg codeAndMsg = new CodeAndMsg();
        if (dl ==null){
            codeAndMsg.setCode(1003);
        }else {
            int key = dingDanDao.insertOneDingDan(count, outprice, dl.getUserid());
            int i = dingDanDao.insertDingDanXQ(bookid, count, outprice, key);
            if (i>0){
                codeAndMsg.setCode(1001);
                codeAndMsg.setMsg("添加成功");
            }else {
                codeAndMsg.setCode(1002);
                codeAndMsg.setMsg("添加失败");
            }
        }
        return codeAndMsg;
    }

    /**
     * 查询所有订单信息和搜索
     * @param dingDan
     * @return
     */
    @Override
    public Result queryAlldd(DingDan dingDan) {
        List<Map<String, Object>> list = dingDanDao.queryAlldd(dingDan);
        int allCount = dingDanDao.getAllCount(dingDan);
        Result result = new Result(allCount, list);
        return result;
    }

    /**
     * 查询一条订单详情信息
     * @param dingDan
     * @return
     */
    @Override
    public Result getonedd(DingDan dingDan) {
        List<Map<String, Object>> list = dingDanDao.queryOnedd(dingDan);
        int oneCount = dingDanDao.getOneCount(dingDan);
        Result result = new Result(oneCount, list);
        return result;
    }

    /**
     * 向 订单 表中 插入 订单 信息
     * 向 订单 详情表中 添加 订单 详情信息
     * 成功插入之后 删除 购物车 表中的信息
     * @param gwcid
     * @param totalcount
     * @param totalprice
     * @return
     */
    @Override
    public List<Map<String, Object>> payment(String gwcid, String totalcount, String totalprice, HttpServletRequest request) {

        //System.out.println(gwcid);

        Dl dl = (Dl)request.getSession().getAttribute("dl");
        //1、根据传过来的 gwcid 查询出 购物车信息
        List<Map<String, Object>> gwcByGwcid = gwcDao.getGWCByGwcid(gwcid);
        //2、根据 传过来的 总价 和 总数量 插入 到 订单表中
        int key = dingDanDao.insertDingDan(totalcount, totalprice,dl.getUserid());
        //3、根据查询出来的 购物车 信息 插入 到 订单 详情表中
        dingDanDao.insertDingDanXQ(gwcByGwcid,key);
        if (key>0){
            //4、根据传过来的 gwcid 删除掉 购物车 中的信息
            gwcDao.deleteGWCByGwcid(gwcid);
        }
        //5、查询购物车 中的信息
        List<Map<String, Object>> allGWC = gwcDao.selectAllGWC(dl.getUserid());
        return allGWC;
    }
    /**
     * 订单审核
     * @param ddid
     * @return
     */
    @Override
    public String shenhe(Integer ddid) {
        int updatestatus = dingDanDao.updatestatus(ddid);
        List<Map<String, Object>> getddxq = dingDanDao.getddxq(ddid);
        if (updatestatus>0){
            int j = 0;
            for (int i = 0; i < getddxq.size(); i++) {
                Integer ddcount = Integer.valueOf(getddxq.get(i).get("count").toString());
                Integer bookid=Integer.valueOf(getddxq.get(i).get("bookid").toString());
                int bookCount = dingDanDao.getBookCount(bookid);
                Integer count = bookCount - ddcount;
                j = dingDanDao.updateBookCount(count, bookid);
            }
            if (j>0){
                return "1001";
            }else {
                return "1002";
            }
        }else {
            return "1002";
        }
    }

    /**
     * 先查询出 该用户的 ddid
     * 根据 ddid 查询出 该用户的 订单详情
     * @param request
     * @return
     */
    @Override
    public List<Map<String, Object>> mydingdanxiangqing(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Dl dl = (Dl)session.getAttribute("dl");
        //System.out.println("service====userid"+dl.getUserid());
        //1、根据该用户的 userid 查询 出 该用户 的 所有ddid
        List<Map<String, Object>> oneDDidByUserid = dingDanDao.getOneDDidByUserid(dl.getUserid());
        //System.out.println("service====AllDDid"+oneDDidByUserid);
        //2、根据 该用户的 所有 ddid 查询出 该用户的  所有的 订单 详情信息
        List<Map<String, Object>> dingDanXQByddid = dingDanDao.getDingDanXQByddid(oneDDidByUserid);
        //System.out.println("service====dingdanxiangqing"+dingDanXQByddid);
        session.setAttribute("dingdanxiangqing",dingDanXQByddid);
        return dingDanXQByddid;
    }
}
